//-----------------------------------------------------------------------------
//クラス名：Timetable.java
//所    属：H3A1 04
//名    前：大瀬碩
//説    明：時間割情報を取得し表示させる
//-----------------------------------------------------------------------------
package timetable;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import javax.servlet.annotation.WebServlet;


//@WebServlet("/Timetable")
public class Timetable extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public Timetable() {
        super();
        
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		
		//DB接続
		ServletContext context = this.getServletContext();
		String path = context.getRealPath("/WEB-INF/connect.properties");
		
		InputStream in = new FileInputStream(path);
		Properties properties = new Properties();
		properties.load(in);
		in.close();
		
		
	 	HttpSession session = request.getSession(true);
	 	
	    Connection conn = null;
	    String url = properties.getProperty("key1");
	    String user = properties.getProperty("key2");
	    String password = properties.getProperty("key3");
	    
	    String i = request.getParameter("class");
	    session.setAttribute("sel_class", i);
	    
	    try {
	      Class.forName("com.mysql.jdbc.Driver").newInstance();
	      conn = DriverManager.getConnection(url, user, password);
	      
	      //DBから取得しJSPに表示させる
	      for(int we=1;we<=5;we++){
	    	  for(int j=1;j<=4;j++){
		      Statement stmt = conn.createStatement();
		      String sql = "SELECT * FROM timetable_details,class_subject,class_room "+
		      "WHERE timetable_details.class_subject_id=class_subject.class_subject_id"+
		      " AND timetable_details.class_room_id=class_room.class_room_id"+
		      " AND class_id = "+i+
		      " AND week_id = "+we+
		      " AND hour_id = "+j;
		      ResultSet rs = stmt.executeQuery(sql);
		      
		      while(rs.next()){
		    	  if(we==1){
		    		  request.setAttribute("mo"+j,rs.getString("class_subject_name"));
		    		  request.setAttribute("mo"+j+"r",rs.getString("class_room_name"));
		    	  }
		    	  
		    	  if(we==2){
		    		  request.setAttribute("tu"+j,rs.getString("class_subject_name"));
		    		  request.setAttribute("tu"+j+"r",rs.getString("class_room_name"));
		    	  }
		    	  
		    	  if(we==3){
		    		  request.setAttribute("we"+j,rs.getString("class_subject_name"));
		    		  request.setAttribute("we"+j+"r",rs.getString("class_room_name"));
		    	  }
		    	  
		    	  if(we==4){
		    		  request.setAttribute("th"+j,rs.getString("class_subject_name"));
		    		  request.setAttribute("th"+j+"r",rs.getString("class_room_name"));
		    	  }
		    	  
		    	  if(we==5){
			    	  request.setAttribute("fr"+j,rs.getString("class_subject_name"));
			    	  request.setAttribute("fr"+j+"r",rs.getString("class_room_name"));
		    	  }
		      }
		      rs.close();
		      stmt.close();
	    	  }
	    }
	    }catch (ClassNotFoundException e){
	    	
	    }catch (SQLException e){
	      
	    }catch (Exception e){
	    	
	    }finally{
	      try{
	        if (conn != null){
	          conn.close();
	        }
	      }catch (SQLException e){
	        
	      }
	    }
	    
	    RequestDispatcher rd = request.getRequestDispatcher("Timetable.jsp");
		rd.forward(request,response);
	}
}
